package com.zijing2333.adaptivescheduling.InteractionWithDeployer;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.zijing2333.adaptivescheduling.IInteractionWithDeployer;

import com.zijing2333.adaptivescheduling.SwitcherManager.SwitcherService;
import com.zijing2333.adaptivescheduling.SwitcherManager.SwitchTaskInfo;

/**
 * SwitcherInteractionWithDeployer 服务类，用于与部署器进行交互。
 */
public class SwitcherInteractionWithDeployer extends Service {
    private static final String TAG = "fun_SwitcherInteractionWith_time";
    public SwitcherInteractionWithDeployer() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new Mybinder();
    }

    public void onCreate() {
        super.onCreate();
    }


    class Mybinder extends IInteractionWithDeployer.Stub{ //进行存储JS任务到切换器，并通知JS管理模块，启动JS任务
        /**ADD
         * 修改调用TaskInfo时使用的参数及类型
         * */
        // 目前是这样想的，state=1时，如果是这个任务是关键任务，那么当前部署的任务就是副本了，如果当前任务不是关键任务，那就是普通任务的切换了
        @Override
        public void sendTaskToSwitcher(String taskId, String url,boolean isFrontendRequired,int state,String sourceIP,int priority) throws RemoteException {

            SwitchTaskInfo switchTaskInfo = new SwitchTaskInfo(taskId,url,isFrontendRequired,priority);

            //将通知传给SwitcherService
            SwitcherService switcherService = SwitcherService.getInstance();
            if (switcherService != null){
                //这里的state是部署器知道的，如果是正常任务部署的话，state为0，如果是需要给对端发送服务已部署通知的任务，state为1
                long timestamp1 = System.currentTimeMillis();
                Log.d(TAG, "B设备部署器触通知切换器，到达切换器的时间:"+timestamp1);
                switcherService.onNotificationReceived(switchTaskInfo,state,sourceIP);
            }else {
                // 处理 mainActivity 为空时的情况
                Log.d(TAG, "SwitcherService为空");
            }

        }
    }
}

